#region Copyright & License
/* Tyng.Data
 * Copyright (c) 2007, Paul Tyng, All rights reserved.
 * Code licensed under the BSD License:
 * http://www.opensource.org/licenses/bsd-license.php
 * Downloaded from: http://code.google.com/p/tyngdata/
*/
#endregion

using System;

namespace Tyng.Data
{
	/// <summary>
	/// Field implementation for the FixedLengthDataReader.
	/// </summary>
    [Serializable]
	public class FixedLengthField : Field
	{
		private int _start;
		private int _length;

		#region Constructors
		/// <summary>
		/// Constructor for FixedLengthField instance
		/// </summary>
		/// <param name="header">Name (Column Header) of the field</param>
		/// <param name="start">The zero-based start position of the data in this column</param>
		/// <param name="length">The length of the data in this column</param>
		public FixedLengthField(string header, int start, int length) : base(header, typeof(string))
		{
			_start = start;
			_length = length;
		}

		/// <summary>
		/// Constructor for FixedLengthField instance
		/// </summary>
		/// <param name="header">Name (Column Header) of the field</param>
		/// <param name="type">Type of the field</param>
		/// <param name="start">The zero-based start position of the data in this column</param>
		/// <param name="length">The length of the data in this column</param>
		public FixedLengthField(string header, Type type, int start, int length) : base(header, type)
		{
			_start = start;
			_length = length;
		}

		/// <summary>
		/// Constructor for FixedLengthField instance
		/// </summary>
		/// <param name="header">Name (Column Header) of the field</param>
		/// <param name="start">The zero-based start position of the data in this column</param>
		/// <param name="length">The length of the data in this column</param>
		/// <param name="ifc">IFieldConverter implementation to convert data</param>
		public FixedLengthField(string header, int start, int length, IFieldConverter ifc) : base(header, typeof(string), ifc)
		{
			_start = start;
			_length = length;
		}

		/// <summary>
		/// Constructor for FixedLengthField instance
		/// </summary>
		/// <param name="header">Name (Column Header) of the field</param>
		/// <param name="type">Type of the field</param>
		/// <param name="start">The zero-based start position of the data in this column</param>
		/// <param name="length">The length of the data in this column</param>
		/// <param name="ifc">IFieldConverter implementation to convert data</param>
		public FixedLengthField(string header, Type type, int start, int length, IFieldConverter ifc) : base(header, type, ifc)
		{
			_start = start;
			_length = length;
		}
		#endregion

		/// <summary>
		/// The zero-based start position of the data in this column
		/// </summary>
		public int Start { get { return _start; } }
		/// <summary>
		/// The length of the data in this column
		/// </summary>
		public int Length { get { return _length ; } }

        /// <summary>
		/// Extract the typed value from the character data
		/// </summary>
		/// <param name="data">Record character data</param>
		/// <returns>Typed value</returns>
		public object ExtractValue(char[] data)
		{
			//Extract the value
			string fieldValue = new string(data, _start, _length);

			return ConvertFromString(fieldValue);
		}
    }
}
